package com.shuzu.demo2;

public class ArrayTest3 {
	public static void main(String[] args) {
		String[] arr = new String[]{"JJ","DD","MM","BB","GG","AA"};
		
		//数组的复制
		String[] arr1 = new String[arr.length];
		for(int i =0;i<arr.length;i++){
			arr1[i] = arr[i];
		}
		
//		//数组的反转
//		for(int i=0;i<arr.length/2;i++){
//			String temp =arr[i];
//			arr[i] = arr[arr.length-i-1];
//			arr[arr.length-i-1] =temp;
//			
//		}
		//数组的遍历
		for(int i =0; i<arr.length;i++){
			System.out.print(arr[i] + "\t");
		}
		
		
		//查找
		//线性查找
//		String dest = "BB";
		String dest = "YY";
		boolean isFlag= true;
		
		for(int i= 0;i<arr.length;i++){
			if(dest.equals(arr[i])){
				System.out.println("俺找到了，位置为"+i);
				isFlag=false;
				break;
			}
			
		}
		if(isFlag){
			System.out.println("没找到");
		}
		
		
		//二分查找
		//前提，查找的数组必须有序
		int[] arr2 =new int[]{-98,-34,2,34,54,66,79,105,210,333};
		
		int dest1 =-38;
		int head = 0;
		int end = arr2.length-1;
		boolean isFlag1=true;
		while(head <= end){
			int middle = (head + end)/2;
			if(dest1 == arr2[middle]){
				System.out.println("俺找到了，位置为"+middle);
				isFlag1=false;
				break;
			}else if(arr2[middle]>dest1){
				end = middle - 1;
			}else if(arr2[middle]<dest1){
				head = middle +1;
			}
			
		}
		if(isFlag1){
			System.out.println("没找到");
		}
				
	}

}
